package com.sh.data.engine.domain.datadev.flink.model.domain;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sh.data.engine.repository.dao.datadev.entity.FlinkNodeConfigEntity;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.Objects;

@Data
public class FlinkTaskDomain {
    /**
     * 主键
     */
    private Long id;

    /**
     * 项目id
     */
    private Long projectId;

    /**
     * 节点id
     */
    private Long nodeId;

    /**
     * 资源id
     */
    private Long resourceNodeId;

    /**
     * main class
     */
    private String mainClass;

    /**
     * 发布状态 0-未发布 1-发布
     */
    private Integer isPublished;

    /**
     * 创建人id
     */
    private String userId;

    /**
     * 修改人id
     */
    private String modifyUserId;

    /**
     * 发布人id
     */
    private String publishUserId;

    /**
     * 发布时间
     */
    private Date publishTime;

    /**
     * 描述
     */
    private String remark;

    /**
     * 是否是最新的记录
     */
    private Integer isActive;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 环境参数配置
     */
    private String envConfig;

    /**
     * 调度配置
     */
    private String scheduleConfig;

    private Date nextExecTime;

    private Date nextStopTime;

    public FlinkScheduleConfigDomain getScheduleConfigDomain() {
        if (StringUtils.isBlank(scheduleConfig)) {
            return null;
        }
        return JSONObject.parseObject(scheduleConfig, FlinkScheduleConfigDomain.class);
    }

    public void init(FlinkNodeConfigEntity entity) {
        this.id = entity.getId();
        this.projectId = entity.getProjectId();
        this.nodeId = entity.getNodeId();
        this.resourceNodeId =
            Objects.isNull(entity.getJarTaskConfig())
                ? null
                : entity.getJarTaskConfig().getResourceNodeId();
        this.mainClass =
            Objects.isNull(entity.getJarTaskConfig())
                ? StringUtils.EMPTY
                : entity.getJarTaskConfig().getMainClass();
        this.isPublished = entity.getIsPublished();
        this.userId = entity.getCreatorId();
        this.modifyUserId = entity.getUpdaterId();
        this.publishUserId = entity.getPublisherId();
        this.publishTime = entity.getPublishTime();
        this.remark = entity.getRemark();
        this.isActive = entity.getIsActive();
        this.createTime = entity.getCreateTime();
        this.updateTime = entity.getUpdateTime();
        this.envConfig = entity.getEnvConfig();
        this.scheduleConfig = JSON.toJSONString(entity.getScheduleConfig());
        this.nextExecTime = entity.getNextExecTime();
        this.nextStopTime = entity.getNextStopTime();
    }
}
